package com.example.demo.controller;

import com.example.demo.base.*;
import com.example.demo.entity.Admin;
import com.example.demo.entity.House;
import com.example.demo.entity.User;
import com.example.demo.service.HouseService;
import com.example.demo.service.UserService;
import com.example.demo.vo.HouseVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Validated
@RequestMapping({"/manage/"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "/user/userList")
    public BaseResponse getUserList(
            @RequestParam(required = false, value = "platForm") String platForm,
            @RequestParam(required = false, value = "userName") String userName,
            @RequestParam(required = false, value = "deleted") Integer deleted,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request) {
        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("state",1));
        if(StringUtils.isNotBlank(platForm)){
            queryBuilder.addFilter(Filter.eq("plat_form",platForm));
        }
        if(StringUtils.isNotBlank(userName)){
            queryBuilder.addFilter(Filter.eq("user_name",userName));
        }
        if(deleted==0){
            queryBuilder.setDeleted(true);
        }
        Pageable<User> pageable = userService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        if(pageable!=null && !CollectionUtils.isEmpty(pageable.getRecords())){
            List<HouseVO> list = new ArrayList<>();
            for(User user:pageable.getRecords()){
                queryBuilder.clear();
                queryBuilder.addFilter(Filter.eq("user_id",user.getId()));
                House house = houseService.selectEntity(queryBuilder);
                HouseVO houseVO = new HouseVO(user,house);
                list.add(houseVO);
            }

            response.setRows(list);
            response.setTotal(new Long(pageable.getTotal()).intValue());
        }
        return response;
    }

    @RequestMapping(value = "/user/update")
    public BaseResponse updateUser(
            @RequestParam(required = false, value = "userId") Long userId,
            @RequestParam(required = false, value = "password") String password,
            @RequestParam(required = false, value = "sex") String sex,
            @RequestParam(required = false, value = "address") String address,
            @RequestParam(required = false, value = "phone") String phone,
            @RequestParam(required = false, value = "floorId") Integer floorId,
            @RequestParam(required = false, value = "unitId") Integer unitId,
            @RequestParam(required = false, value = "roomId") String roomId,
            @RequestParam(required = false, value = "houseId") String houseId,
            @RequestParam(required = false, value = "sign") Integer sign,
            HttpServletRequest request) {
        User user = new User();
        House house = new House();
        if(sign==0){//删除
            user.setDeleted(1);
        }else if(sign==1){
            user.setPassword(com.example.demo.util.StringUtils.MD5Encode(password));
            house.setSex(sex);
            house.setAddress(address);
            house.setPhone(phone);
            house.setFloorId(floorId);
            house.setUnitId(unitId);
            house.setRoomId(roomId);
            house.setHouseId(houseId);
        }else{
            user.setDeleted(0);
        }
        UpdateBuilder updateBuilder = new UpdateBuilder(user);
        updateBuilder.addFilter(Filter.eq("id",userId));
        userService.updateEntity(updateBuilder);

        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("user_id",userId));
        House house1 = houseService.selectEntity(queryBuilder);
        if(house1 == null){
            house.setUserId(userId);
            houseService.insertEntity0(house);
        }else{
            updateBuilder.clear();
            updateBuilder.setEntity(house);
            updateBuilder.addFilter(Filter.eq("user_id",userId));
            houseService.updateEntity(updateBuilder);
        }
        return BaseResponse.success();
    }


    @RequestMapping(value = "/user/add")
    public BaseResponse addUser(
            @RequestParam(required = false, value = "platForm") String platForm,
            @RequestParam(required = false, value = "userName") String userName,
            @RequestParam(required = false, value = "password") String password,
            @RequestParam(required = false, value = "state") Integer state,
            HttpServletRequest request) {
        User user = new User();
        user.setPlatForm(platForm);
        user.setUserName(userName);
        user.setPassword(com.example.demo.util.StringUtils.MD5Encode(password));
        user.setState(state);
        userService.insertEntity0(user);
        return BaseResponse.success();
    }
}
